package com.weihuachao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lidroid.xutils.http.RequestParams;
import com.weihuachao.app.WorkAdapter;
import com.weihuachao.app.application;
import com.weihuachao.entity.Result;
import com.weihuachao.entity.Results;
import com.weihuachao.entity.User;
import com.weihuachao.entity.Work;
import com.weihuachao.https.BasicRequestCallBck;
import com.weihuachao.https.WorkRequestCallBck;
import com.weihuachao.https.XUtils;
import com.weihuachao.repair.MainActivity;
import com.weihuachao.repair.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Virgo on 2016/7/30.
 */
public class Main_fragment extends Fragment {
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private View mLayout;
    private ListView listView;
    private WorkAdapter adapter;
    private List<Work> workList = new ArrayList<Work>();
    private int workState = 0;

    public Main_fragment() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        XUtils.init(getActivity());
        if (mLayout == null) {
            mLayout = inflater.inflate(R.layout.fragment_main, container, false);
        }
        radioGroup = (RadioGroup) mLayout.findViewById(R.id.changestate);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int radioid = radioGroup.getCheckedRadioButtonId();
                int tmp = 0;
                if (radioid == R.id.radioButton1) {
                    tmp = 0;
                } else if (radioid == R.id.radioButton2) {
                    tmp = 1;
                } else {
                    tmp = 2;
                }
                if (workState != tmp) {
                    workState=tmp;
                    initData();
                    adapter.notifyDataSetChanged();
                }
            }
        });
        workState = 0;
        initData();
        listView = (ListView) mLayout.findViewById(R.id.listView);
        adapter = new WorkAdapter(this.getActivity(), R.layout.item, workList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(Main_fragment.this.getActivity(), String.valueOf(workList.get(i).getText()), Toast.LENGTH_SHORT).show();
            }
        });
        adapter.notifyDataSetChanged();
        return mLayout;
    }


    private void initData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("work.state", String.valueOf(workState));
        params.addBodyParameter("work.uid", String.valueOf(0));
        XUtils.send(XUtils.GETWORK, params, new WorkRequestCallBck() {
                    @Override
                    public void success(List<Work> data) {
                        workList.clear();
                        for (int i = 0; i < data.size(); ++i) {
                            workList.add(data.get(i));
                        }
                    }
                }
        );
    }
}
